# coding=utf-8
import allure
from common.assert_operator import *
from common.db import *
from common.read_yaml import *
from common.utils import Utils

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
data = ReadYaml_By_Root_Dict("case/99_outside_api_test/02_bpm_outside_test/99_02_3_DataStorageSave_TriggerFlowEvent.yml").get_yaml_data()  # 读取数据

def TriggerFlowEvent_SQL(BussinessGUID,EventName):
    """
    触发业务事件后查询数据库sql，触发业务事件后，校验查询结果
    :param BussinessGUID: 业务GUID
    :param EventName:   事件名称
    """
    Query_SQL = f"""SELECT package_id,process_instance_id,process_name,event_name,status FROM wf_lg_trigger_event  lg WITH (NOLOCK)
                        INNER JOIN dbo.WfProcessInstance pe WITH (NOLOCK) ON cast(pe.Id as varchar(36)) = lg.process_instance_id
                        WHERE pe.BusinessKey='{BussinessGUID}' AND lg.event_name='{EventName}';"""
    return Query_SQL

@allure.feature('测试流程中心外部接口：保存表单数据接口，触发业务事件')  # 测试报告显示测试功能
class Test_99_02_3_DataStorageSave_TriggerFlowEvent():
    """
    测试接口：
    /api/flowdefine/data-storage/save
    /api/flowdefine/flow-event/trigger-flow-event
    """
    # 获取yml文件中[保存表单数据]的数据参数
    Params = data['保存表单数据']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_DataStorageSave(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Bpm_Outside_Api(param['用户'])
        resp = main.data_storage_save(param['请求体'])
        dict_assert(expect['响应校验'], resp.json())  # 结果断言
        # 成功后，插入数据到bpm_MysoftERP_htsxcpsp表
        sql = f"""SELECT proc_id '流程GUID', business_key '业务GUID',sxcpbh_single_text '事项呈批编号',sxcpmc_single_text '事项呈批名称',sqbm_single_text '申请部门'
FROM bpm_MysoftERP_htsxcpsp  WITH (NOLOCK) WHERE proc_id='{param['请求体']['ProccessInstanceID']}'"""
        result = sqlserver_query_return_contain_field(sql)
        list_each_equal(expect['数据库校验'],result)


    # 获取yml文件中[保存表单数据_异常场景]的数据参数
    Params = data['保存表单数据_异常场景']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_DataStorageSave_Abnormal(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        main = user_login.goto_Bpm_Outside_Api(param['用户'])
        resp = main.data_storage_save(param['请求体'])
        dict_assert(expect['响应校验'],resp.json())    # 结果断言

    # 获取yml文件中[触发业务事件]的数据参数
    Params = data['触发业务事件']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_TriggerFlowEvent(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        user, EventName, HandleType, BussinessGUID, OtherParams = param['用户'], param['事件名称'], \
                                                                           param['处理类型'], param['业务GUID'], \
                                                                           param['其他参数']
        main = user_login.goto_Bpm_Outside_Api(user)
        # 发起触发业务时间接口请求
        resp = main.trigger_flow_event(user, EventName, HandleType, BussinessGUID, OtherParams)
        dict_assert(expect['响应校验'],resp.json())    # 结果断言
        # 执行后，断言数据库结果
        sql = TriggerFlowEvent_SQL(BussinessGUID, EventName)
        result = sqlserver_query_return_contain_field(sql)  # 查询结果
        list_each_equal(expect['数据库校验'],result)    # 结果断言

    # 获取yml文件中[触发业务事件_异常场景]的数据参数
    Params = data['触发业务事件_异常场景']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_TriggerFlowEvent_Abnormal(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['用例描述']}")  # # 动态解析用例描述在测试报告中展示
        user, EventName, HandleType, BussinessGUID, OtherParams = param['用户'], param['事件名称'], \
                                                                  param['处理类型'], param['业务GUID'], \
                                                                  param['其他参数']
        main = user_login.goto_Bpm_Outside_Api(user)
        # 发起触发业务时间接口请求
        resp = main.trigger_flow_event(user, EventName, HandleType, BussinessGUID, OtherParams)
        dict_assert(expect['响应校验'], resp.json())  # 结果断言
        # 执行后，断言数据库结果
        sql = TriggerFlowEvent_SQL(BussinessGUID, EventName)
        result = sqlserver_query_return_contain_field(sql)  # 查询结果
        list_each_equal(expect['数据库校验'], result)  # 结果断言
